<template>
  <div>
    <el-page-header @back="back" :content="course.name" title="返回">
    </el-page-header>
    <p style="display: flex">
      <el-button type="info" icon="el-icon-refresh" size="mini"
                 @click="list()">刷新
      </el-button>
      <el-button type="info" size="mini"
                 @click="dialogVisible=true">添加
      </el-button>
      <el-button type="info" icon="el-icon-refresh" size="mini"
                 @click="list()">刷新
      </el-button>
    </p>
    <p style="display: flex">
      <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageInfo.size"
          :current-page="pageInfo.page"
          @current-change="list"
          :total="pageInfo.total">
      </el-pagination>
      <el-select v-model="pageInfo.size" placeholder="请选择"
                 @change="list(1)">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"

            :value="item.value">
        </el-option>
      </el-select>
    </p>
    <div>
      <el-dialog title="添加大章" :visible.sync="dialogVisible">
        <el-form :model="form">
          <el-form-item label="大章名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="课程" :label-width="formLabelWidth">
            {{ course.name }}
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogCancel">取 消</el-button>
          <el-button type="primary" @click="saveChapter">确 定</el-button>
        </div>
      </el-dialog>
      <el-table
          :data="pageInfo.list"
          style="width: 100%">
        <el-table-column
            prop="id"
            label="ID"
            width="180">
        </el-table-column>
        <el-table-column
            prop="courseId"
            label="课程ID"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="课程名">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="goSection(scope.row)">小节</el-button>
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
</template>

<script>
import {chapter, deleteChapter, saveChapter} from "@/network/chapter";
import {Confirm} from '@/assets/js/confirm'
import {Validator} from "@/assets/js/validator";

export default {
  name: "chapter",
  mounted() {
    let _this = this;
    _this.pageInfo.size = 5;
    let course = SessionStorage.get("course") || {};
    if (Tool.isEmpty(course)) {
      _this.router.push("/welcome");
    }
    _this.course = course;

    _this.pageInfo.courseId = course.id;
    this.list(1)
  },
  data() {
    return {
      course: {},
      pageInfo: {
        page: 1,
        size: 1,
        list: [],
        total: 0,
        courseId: '',
      }, options: [
        {
          value: 1,
          label: 1
        },
        {
          value: 5,
          label: 5
        },
        {
          value: 10,
          label: 10
        },
        {
          value: 50,
          label: 50
        },],
      dialogVisible: false,
      form: {},
      formLabelWidth: '120px'
    }
  },
  methods: {
    list(page) {
      if (page === null || page === undefined) {
        page = this.pageInfo.page
      }
      let _this = this;
      _this.pageInfo.list = [];
      chapter(_this.pageInfo).then(res => {
        _this.pageInfo = res.data
      })
    },
    saveChapter() {
      let _this = this;

      if (Validator.length(_this.form.name, "课程名字", 1, 8) === false) {
        return;
      }
      _this.form.courseId = _this.course.id;

      saveChapter(_this.form).then(res => {
        _this.$message.success("保存成功");
        _this.form = {}
        _this.dialogVisible = false;
        _this.list()
      })
    },
    edit(row) {
      console.log(row);
      let _this = this;
      _this.form = Object.assign({}, row)
      _this.dialogVisible = true;
    },
    del(row) {
      let _this = this;
      Confirm.show('此操作将永久删除该文件, 是否继续?', function () {
        deleteChapter(row.id).then(res => {
          _this.list();
          _this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      })
    },
    dialogCancel() {
      let _this = this;
      _this.form = {};
      _this.dialogVisible = false;
    },
    back() {
      this.$router.push("/admin/course")
    },
    goSection(chapter) {
      let _this = this;
      SessionStorage.set("chapter", chapter);
      this.$router.push("/admin/section")
    }
  }
}
</script>

<style scoped>

</style>
